﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Corona
{
	/// <summary>
	/// DrawSelect.xaml 的交互逻辑
	/// </summary>
	public partial class DrawSelect
	{
        public double SideLineThickness = 2; 
        private bool IsDrawing = false;
        /// <summary>
        /// 初始点
        /// </summary>
        private Point initialPoint;
        /// <summary>
        /// 起始点
        /// </summary>
        private Point startPoint;
        /// <summary>
        /// 结束点
        /// </summary>
        private Point endPoint;
        /// <summary>
        /// 终结点
        /// </summary>
        private Point finalPoint;
        public PointCollection SelectorPoints = new PointCollection();
        public delegate void AddSelectorPointHandle(object sender);
        public event AddSelectorPointHandle AddSelectorPoint;

        //保留字段，用于获取选区大小
        //private double MostLeft;
        //private double MostRight;
        //private double MostTop;
        //private double MostBottom;

        /// <summary>
        /// 绘制选区初始化初始化
        /// </summary>
		public DrawSelect()
		{
			this.InitializeComponent();
		}

        public void InitialSelector(Point InitialPoint)
        {
            SelectorPoints.Clear();
            IsDrawing = true;
            initialPoint = InitialPoint;
            startPoint = InitialPoint;
            SelectorPoints.Add(initialPoint);
            //#region findMost
            //MostLeft = MostRight = startPoint.X;
            //MostTop = MostBottom = startPoint.Y;
            //#endregion
        }

        private void DrawSelector(object sender, MouseEventArgs e)
        {
            Canvas canv = sender as Canvas;
            endPoint = e.GetPosition(canv);
            if (Math.Sqrt(Math.Pow(startPoint.X - endPoint.X, 2) + Math.Pow(startPoint.Y - endPoint.Y, 2)) >= 10 && IsDrawing)
            {
                //多边形中添加新的点
                SelectorPoints.Add(endPoint);
                if (AddSelectorPoint != null)
                    AddSelectorPoint(this);
                //绘制边线
                Line line = new Line();
                line.X1 = startPoint.X;
                line.Y1 = startPoint.Y;
                line.X2 = endPoint.X;
                line.Y2 = endPoint.Y;
                line.StrokeThickness = SideLineThickness;
                line.Stroke = Brushes.OrangeRed;
                canv.Children.Add(line);
                //绘制三角形
                Polygon poly = new Polygon();
                //poly.Points = SelectorPoints;
                poly.Points.Add(initialPoint);
                poly.Points.Add(startPoint);
                poly.Points.Add(endPoint);
                poly.Fill = Brushes.Orange;
                poly.Opacity = 0.2;
                canv.Children.Add(poly);
                //将Start点设为End点，构成链
                startPoint = endPoint;
                //#region findMost
                //if (startPoint.X > MostRight)
                //    MostRight = startPoint.X;
                //else if (startPoint.X < MostLeft)
                //    MostLeft = startPoint.X;
                //if (startPoint.Y > MostBottom)
                //    MostBottom = startPoint.Y;
                //else if (startPoint.Y < MostTop)
                //    MostTop = startPoint.Y;
                //#endregion
            }
        }

        private void EndDrawSelector(object sender, MouseButtonEventArgs e)
        {
            Canvas canv = sender as Canvas;
            finalPoint = e.GetPosition(canv);
            IsDrawing = false;

            //绘制最后一条边线
            Line line = new Line();
            line.X1 = initialPoint.X;
            line.Y1 = initialPoint.Y;
            line.X2 = finalPoint.X;
            line.Y2 = finalPoint.Y;
            line.Stroke = Brushes.OrangeRed;
            line.StrokeThickness = SideLineThickness;
            
            //将最后一点和首点连接起来，构成封闭区域
            Line line2 = new Line();
            line2.X1 = finalPoint.X;
            line2.Y1 = finalPoint.Y;
            line2.X2 = startPoint.X;
            line2.Y2 = startPoint.Y;
            line2.Stroke = Brushes.OrangeRed;
            line2.StrokeThickness = SideLineThickness;
            
            //绘制最后一块三角形
            Polygon poly = new Polygon();
            poly.Points.Add(initialPoint);
            poly.Points.Add(startPoint);
            poly.Points.Add(finalPoint);
            poly.Fill = Brushes.Orange;
            poly.Opacity = 0.2;
            canv.Children.Add(poly);
            canv.Children.Add(line2);
            canv.Children.Add(line);
        }

        /// <summary>
        /// 清除Canvas上的所有元素
        /// </summary>
        public void ClearSelector()
        {
            ParentCanvas.Children.Clear();
        }
	}
}